package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BusCoutMapper;
import com.ruoyi.system.domain.BusCout;
import com.ruoyi.system.service.IBusCoutService;
import com.ruoyi.common.core.text.Convert;

/**
 * 合伙人收益Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@Service
public class BusCoutServiceImpl implements IBusCoutService
{
    @Autowired
    private BusCoutMapper busCoutMapper;

    /**
     * 查询合伙人收益
     *
     * @param id 合伙人收益主键
     * @return 合伙人收益
     */
    @Override
    public BusCout selectBusCoutById(String id)
    {
        return busCoutMapper.selectBusCoutById(id);
    }

    /**
     * 查询合伙人收益列表
     *
     * @param busCout 合伙人收益
     * @return 合伙人收益
     */
    @Override
    public List<BusCout> selectBusCoutList(BusCout busCout)
    {
        return busCoutMapper.selectBusCoutList(busCout);
    }

    /**
     * 新增合伙人收益
     *
     * @param busCout 合伙人收益
     * @return 结果
     */
    @Override
    public int insertBusCout(BusCout busCout)
    {
        return busCoutMapper.insertBusCout(busCout);
    }

    /**
     * 修改合伙人收益
     *
     * @param busCout 合伙人收益
     * @return 结果
     */
    @Override
    public int updateBusCout(BusCout busCout)
    {
        return busCoutMapper.updateBusCout(busCout);
    }

    /**
     * 批量删除合伙人收益
     *
     * @param ids 需要删除的合伙人收益主键
     * @return 结果
     */
    @Override
    public int deleteBusCoutByIds(String ids)
    {
        return busCoutMapper.deleteBusCoutByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除合伙人收益信息
     *
     * @param id 合伙人收益主键
     * @return 结果
     */
    @Override
    public int deleteBusCoutById(String id)
    {
        return busCoutMapper.deleteBusCoutById(id);
    }

    @Override
    public String importBusCout(List<BusCout> list, Boolean isUpdateSupport, String operName) {
        //判断传入的Excel 是否为空
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        //定义失败条数，接收导入失败数据量
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder ( );
        StringBuilder failureMsg = new StringBuilder ( );
        //遍历导入数据，并且插入数据库。
        for ( BusCout busCout : list ) {
            busCout.setId(IdUtils.randomUUID());
            this.insertBusCout(busCout);
        }
        if ( failureNum > 0 ) {
            failureMsg.insert ( 0 , "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：" );
            throw new ServiceException ( failureMsg.toString ( ) );
        } else {
            successMsg.insert ( 0 , "数据已全部导入成功！共 " + list.size () + " 条，数据如下：" +"\n"+list.toString ());
        }
        return successMsg.toString ();
    }
}
